﻿using System;
using System.Data;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class AddDimension : System.Web.UI.Page
    {
        private string dimension_id = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li3.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Clear();
            ((SiteSystemMaster)Master).s_menu3_1.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu3_1.Style.Add("TEXT-DECORATION", "none"); 

            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    DimensionBLL dimensionBll = new DimensionBLL();
                    DataSet ds = dimensionBll.GetDimensionClass();
                    ddlType.DataSource = ds.Tables[0].DefaultView;
                    ddlType.DataTextField = "dimension_class_nm";
                    ddlType.DataValueField = "dimension_class_id";
                    ddlType.DataBind();

                    dimension_id = Request.QueryString["dimension_id"];

                    if (!string.IsNullOrEmpty(dimension_id))
                    {
                        Dimension di = dimensionBll.GetDimension(dimension_id);

                        txtbDimnm.Text = di.Dimnm;
                        txtbDescrip.Text = di.Dimension_descrip;
                        ddlType.SelectedValue = di.Dimension_classid.ToString();
                        txtbTime.Text = (di.Time_min).ToString();

                        txtbQ1.Text = di.Sinterval.ToString();
                        txtbQ2.Text = di.Minterval.ToString();
                        txtbQ3.Text = di.Linterval.ToString();

                        txtb2.Text = di.Dimension_2.ToString();
                        txtb8.Text = di.Dimension_8.ToString();
                        txtb17.Text = di.Dimension_17.ToString();

                        txtb32.Text = di.Dimension_32.ToString();
                        txtb50.Text = di.Dimension_50.ToString();
                        txtb68.Text = di.Dimension_68.ToString();

                        txtb83.Text = di.Dimension_83.ToString();
                        txtb92.Text = di.Dimension_92.ToString();
                        txtb98.Text = di.Dimension_98.ToString();

                        txtXiaXianTime.Text = di.Time_xiaxian.ToString();
                        txtShangXianTime.Text = di.Time_shangxian.ToString();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DimensionBLL dimensionBll = new  DimensionBLL();
            Dimension temp = new  Dimension();

            if (Request.QueryString["dimension_id"] != null)
            {
                temp.Dimension_id = int.Parse(Request.QueryString["dimension_id"]);
            }
           
            temp.Dimnm =txtbDimnm.Text.Trim();
            temp.Dimension_descrip = txtbDescrip.Text.Trim();
            temp.Dimension_classid=int.Parse(ddlType.SelectedValue);
            temp.Time_min = int.Parse(txtbTime.Text.Trim());//界面输入值为分，DB存储秒

            temp.Linterval = float.Parse(txtbQ3.Text.Trim());
            temp.Minterval = float.Parse(txtbQ2.Text.Trim());
            temp.Sinterval = float.Parse(txtbQ1.Text.Trim());

            temp.Dimension_2 = float.Parse(txtb2.Text.Trim());
            temp.Dimension_8 = float.Parse(txtb8.Text.Trim());
            temp.Dimension_17 = float.Parse(txtb17.Text.Trim());

            temp.Dimension_32 = float.Parse(txtb32.Text.Trim());
            temp.Dimension_50 = float.Parse(txtb50.Text.Trim());
            temp.Dimension_68 = float.Parse(txtb68.Text.Trim());

            temp.Dimension_83 = float.Parse(txtb83.Text.Trim());
            temp.Dimension_92 = float.Parse(txtb92.Text.Trim());
            temp.Dimension_98 = float.Parse(txtb98.Text.Trim());

            //维度上限时间，下限时间
            temp.Time_xiaxian = int.Parse(txtXiaXianTime.Text.Trim());
            temp.Time_shangxian = int.Parse(txtShangXianTime.Text.Trim());
            
            if (dimensionBll.Exists(temp.Dimension_id))
            {
                int n = dimensionBll.Update(temp);
                if (n > 0)
                {
                    MessageBox.ShowAndRedirect(this, "维度修改成功", "DimensionList.aspx");
                }
            }
            else
            {
                if (dimensionBll.Add(temp) != -1)
                {
                    MessageBox.ShowAndRedirect(this, "维度添加成功", "DimensionList.aspx");
                }
            }
        }
    }
}